using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Walkmen.ID3v1;
using Walkmen.ID3v2;
using Walkmen.OpenMG;
using Walkmen.Convert;
using Walkmen.Properties;

namespace Walkmen
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            this.listMusic.ListViewItemSorter = new SongItemComparer();
        }

        private void btnFrom_Click(object sender, EventArgs e)
        {
            if (fbFrom.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            txtFrom.Text = fbFrom.SelectedPath;

            string[] files = Directory.GetFiles(txtFrom.Text, "*.oma", SearchOption.AllDirectories);
            Array.Sort(files);

            progressBar.Maximum = files.Length;
            progressBar.Minimum = 0;
            progressBar.Value = 0;
            progressBar.Step = 1;

            try
            {
                this.Enabled = false;
                this.Cursor = Cursors.WaitCursor;

                listMusic.BeginUpdate();
                listMusic.Items.Clear();

                foreach (string file in files)
                {
                    try
                    {
                        Song song = Song.Parse(file);

                        ListViewItem item = new ListViewItem(song.Album);
                        item.SubItems.Add(song.Track.ToString());
                        item.SubItems.Add(song.Title);
                        item.SubItems.Add(song.Artist);
                        item.SubItems.Add(file);
                        item.Tag = file;
                        listMusic.Items.Add(item);
                    }
                    catch (FormatException)
                    {
                    }
                    catch (NotSupportedException)
                    {
                    }

                    progressBar.PerformStep();
                    Application.DoEvents();
                }
            }
            finally
            {
                listMusic.EndUpdate();
                this.Cursor = Cursors.Default;

                this.Enabled = true;
            }
        }

        private void btnTo_Click(object sender, EventArgs e)
        {
            if (fbTo.ShowDialog() == DialogResult.OK)
            {
                txtTo.Text = fbTo.SelectedPath;
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtTo.Text))
            {
                MessageBox.Show(
                    Resources.WARN_SELECT_OUTPUT, 
                    Resources.TERM_APPNAME,
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Warning);
                return;
            }

            progressBar.Maximum = listMusic.SelectedItems.Count;
            progressBar.Minimum = 0;
            progressBar.Value = 0;
            progressBar.Step = 1;

            try
            {
                this.Enabled = false;
                this.Cursor = Cursors.WaitCursor;

                OMAConverter converter = new OMAConverter(txtTo.Text, Settings.Default.ID3Encoding);

                foreach (ListViewItem item in listMusic.SelectedItems)
                {
                    converter.Convert((string)item.Tag);

                    item.Selected = false;

                    progressBar.PerformStep();
                    Application.DoEvents();
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
                this.Enabled = true;
            }

            MessageBox.Show(
                Resources.INFO_CONVERT_FINISH,
                Resources.TERM_APPNAME,
                MessageBoxButtons.OK, 
                MessageBoxIcon.Information);
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            ConfigDialog dialog = new ConfigDialog();
            dialog.ShowDialog();
        }
    }
}